package la.iok.hzsvn.lewin.movie.controller;

import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.entity.MovieFile;
import la.iok.hzsvn.lewin.movie.service.*;
import la.iok.hzsvn.lewin.mybatis.model.PageVo;
import la.iok.hzsvn.share.response.OperatorResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 空闲没有使用的文件进行处理
 */
@RestController
@RequestMapping("freeFiles")
public class FreeFileController {
    private final Map<String, MovieFileService<? extends MovieFile>> serviceMap = new HashMap<>();
    public FreeFileController(AppFileService appFileService,
                              ClueCardFileService clueCardFileService,
                              MovieAssistFileService movieAssistFileService,
                              MovieCoverFileService movieCoverFileService,
                              MovieSceneFileService movieSceneFileService,
                              PlayRoleBookFileService playRoleBookFileService,
                              PlayRoleFileService playRoleFileService) {
        serviceMap.put("app", appFileService);
        serviceMap.put("clueCard", clueCardFileService);
        serviceMap.put("movieAssist", movieAssistFileService);
        serviceMap.put("movieCover", movieCoverFileService);
        serviceMap.put("movieScene", movieSceneFileService);
        serviceMap.put("playRoleBook", playRoleBookFileService);
        serviceMap.put("playRole", playRoleFileService);
    }

    @GetMapping("{type}")
    public SingleDataResponse<PageVo<? extends MovieFile>> freeFiles(@PathVariable String type, int page, int limit, Integer fileDeleted){
        MovieFileService<? extends MovieFile> service = serviceMap.get(type);
        PageVo<? extends MovieFile> pageVo = service.freeFiles(page, limit, fileDeleted);
        return new SingleDataResponse<>(pageVo);
    }

    @DeleteMapping("{type}/{id}")
    public OperatorResponse deleteFreeFile(@RequestHeader Token token, @PathVariable String type, @PathVariable Long id){
        MovieFileService<? extends MovieFile> service = serviceMap.get(type);
        service.deleteFreeFile(token, id);
        return OperatorResponses.success();
    }

    @PutMapping("{type}/{id}")
    public OperatorResponse markFreeFileDeleted(@RequestHeader Token token, @PathVariable String type, @PathVariable Long id) {
        MovieFileService<? extends MovieFile> service = serviceMap.get(type);
        service.markFreeFileDeleted(token, id);
        return OperatorResponses.success();
    }
}
